package com.axeiya.cheddargetter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;

public class PricingPlan {

	private String code;

	private String name;

	private String description;

	private boolean isActive;

	private boolean isFree;

	private int trialDays;

	private String billingFrequency;

	private String billingFrequencyPer;

	private String billingFrequencyUnit;

	private int billingFrequencyQuantity;

	private String setupChargeCode;

	private float setupChargeAmount;

	private String recurringChargeCode;

	private float recurringChargeAmount;

	private Date createdDateTime;

	private List<Item> items;
	
	/**
	 * Constructor 
	 */
	public PricingPlan(String code){
		this.code = code;
	}

	/**
	 * READ CONSTRUCTOR
	 */
	public PricingPlan(String code, String name, String description, boolean isActive,
			boolean isFree, int trialDays, String billingFrequency, String billingFrequencyPer,
			String billingFrequencyUnit, int billingFrequencyQuantity, String setupChargeCode,
			float setupChargeAmount, String recurringChargeCode, float recurringChargeAmount,
			Date createdDateTime, List<Item> items) {
		super();
		this.code = code;
		this.name = name;
		this.description = description;
		this.isActive = isActive;
		this.isFree = isFree;
		this.trialDays = trialDays;
		this.billingFrequency = billingFrequency;
		this.billingFrequencyPer = billingFrequencyPer;
		this.billingFrequencyUnit = billingFrequencyUnit;
		this.billingFrequencyQuantity = billingFrequencyQuantity;
		this.setupChargeCode = setupChargeCode;
		this.setupChargeAmount = setupChargeAmount;
		this.recurringChargeCode = recurringChargeCode;
		this.recurringChargeAmount = recurringChargeAmount;
		this.createdDateTime = createdDateTime;
		this.items = items;
	}

	public static PricingPlan parse(Element plan) {
		String code = plan.getAttributeValue("code");
		String name = plan.getChildText("name");
		String description = plan.getChildText("description");
		boolean isActive = CGTools.parseBoolean(plan.getChildText("isActive"));
		boolean isFree = CGTools.parseBoolean(plan.getChildText("isFree"));
		int trialDays = Integer.valueOf(plan.getChildText("trialDays"));
		String billingFrequency = plan.getChildText("billingFrequency");
		String billingFrequencyPer = plan.getChildText("billingFrequencyPer");
		String billingFrequencyUnit = plan.getChildText("billingFrequencyUnit");
		Integer billingFrequencyQuantity = Integer.valueOf(plan
				.getChildText("billingFrequencyQuantity"));
		String setupChargeCode = plan.getChildText("setupChargeCode");
		float setupChargeAmount = Float.parseFloat(plan.getChildText("setupChargeAmount"));
		String recurringChargeCode = plan.getChildText("recurringChargeCode");
		float recurringChargeAmount = Float.parseFloat(plan.getChildText("recurringChargeAmount"));

		Date createdDateTime = CGTools.parseDateTime(plan.getChildText("createdDatetime"));
		List<Element> itemsList;
		ArrayList<Item> planItemsList = new ArrayList<Item>();
		try {
			itemsList = XPath.selectNodes(plan, "items/item");

			for (Element item : itemsList) {

				Item pricingPlanItem = Item.parseItem(item);
				planItemsList.add(pricingPlanItem);
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new PricingPlan(code, name, description, isActive, isFree, trialDays,
				billingFrequency, billingFrequencyPer, billingFrequencyUnit,
				billingFrequencyQuantity, setupChargeCode, setupChargeAmount, recurringChargeCode,
				recurringChargeAmount, createdDateTime, planItemsList);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public boolean isFree() {
		return isFree;
	}

	public void setFree(boolean isFree) {
		this.isFree = isFree;
	}

	public int getTrialDays() {
		return trialDays;
	}

	public void setTrialDays(int trialDays) {
		this.trialDays = trialDays;
	}

	public String getBillingFrequency() {
		return billingFrequency;
	}

	public void setBillingFrequency(String billingFrequency) {
		this.billingFrequency = billingFrequency;
	}

	public String getBillingFrequencyPer() {
		return billingFrequencyPer;
	}

	public void setBillingFrequencyPer(String billingFrequencyPer) {
		this.billingFrequencyPer = billingFrequencyPer;
	}

	public String getBillingFrequencyUnit() {
		return billingFrequencyUnit;
	}

	public void setBillingFrequencyUnit(String billingFrequencyUnit) {
		this.billingFrequencyUnit = billingFrequencyUnit;
	}

	public int getBillingFrequencyQuantity() {
		return billingFrequencyQuantity;
	}

	public void setBillingFrequencyQuantity(int billingFrequencyQuantity) {
		this.billingFrequencyQuantity = billingFrequencyQuantity;
	}

	public String getSetupChargeCode() {
		return setupChargeCode;
	}

	public void setSetupChargeCode(String setupChargeCode) {
		this.setupChargeCode = setupChargeCode;
	}

	public float getSetupChargeAmount() {
		return setupChargeAmount;
	}

	public void setSetupChargeAmount(float setupChargeAmount) {
		this.setupChargeAmount = setupChargeAmount;
	}

	public String getRecurringChargeCode() {
		return recurringChargeCode;
	}

	public void setRecurringChargeCode(String recurringChargeCode) {
		this.recurringChargeCode = recurringChargeCode;
	}

	public float getRecurringChargeAmount() {
		return recurringChargeAmount;
	}

	public void setRecurringChargeAmount(float recurringChargeAmount) {
		this.recurringChargeAmount = recurringChargeAmount;
	}

	public Date getCreatedDateTime() {
		return createdDateTime;
	}

	public void setCreatedDateTime(Date createdDateTime) {
		this.createdDateTime = createdDateTime;
	}

	public List<Item> getItems() {
		return items;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

}
